using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VICC_FOLDERS
    {
        public void SetUpNewRow(ref DataRow dwReturn, DataRow dwInput = null)
        {
            string time = "";
            Helpers.Helper.GetTime(ref date, ref time);

            switch (app.company)
            {
                default:
                case "YGR":
                    dwReturn["MANDANT"] =app.Mandant;
                    dwReturn["ROWNID"] = "";
                    dwReturn["LINKER"] = " ".PadRight(20);
                    dwReturn["COMPANY"] = app.company;
                    dwReturn["VERSION"] = "";
                    dwReturn["ROOT"] = "";
                    dwReturn["SUB"] = "";
                    break;
            }
        }
        public void InsertICC_FOLDERS(DataRow dwData, object value)
        {
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "INSERT INTO ICC_FOLDERS ( MANDANT,ROWNID,COMPANY,ROOT,SUB,VERSION,FBCONTENT)"
                   + " VALUES (:MANDANT:,:ROWNID:,:COMPANY:,:ROOT:,:SUB:,:VERSION:,:FBCONTENT)";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData); 
            or1.Parameters.Add("FBCONTENT", OracleType.Blob).Value =value;
            // Helper.CompressString(strOriginText)
            Connect2Proxy(or1, "ORACLEBLOB");
        }
        public DataSet QueryICC_FOLDERS(DataRow dwData, int option = 1)
        {

            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM ICC_FOLDERS WHERE  ROWNUM<2 ";
                    break;

                case 99:
                    or1.CommandText = "SELECT * FROM ICC_FOLDERS";
                    break;
                default:
                    break;
            }
            SResult = Connect2Proxy(or1);
            SResult.Tables[0].TableName = "ICC_FOLDERS";
            return SResult;
        }
        public string GETQCMD(DataRow dwData)
        {
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "SELECT MANDANT, ROWNID, COMPANY, ROOT, SUB, VERSION, FBCONTENT FROM ICC_FOLDERS WHERE  MANDANT=:MANDANT: AND (RTRIM(VERSION)='FOLDERS' OR VERSION>:VERSION:)"
                + " ORDER BY 3,4,5,6";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            return or1.CommandText;
        }
        public DataSet QFOLDER(DataRow dwData, int idx, int rows)
        {
            or1 = OracleDB.CreateCommand();
            string sql = GETQCMD(dwData);
            or1.CommandText = "SELECT * FROM (SELECT X.*, ROWNUM RN FROM (" + sql + ") X WHERE  ROWNUM < " + (idx + 1) * rows
                + ")WHERE RN >= " + idx * rows;
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            SResult = Connect2Proxy(or1);
            SResult.Tables[0].TableName = TableName;
            return SResult;
        }
        public DataSet QFALL(DataRow dwData)
        { 
             or1 = OracleDB.CreateCommand();
            or1.CommandText = "SELECT MANDANT, ROWNID, COMPANY, ROOT, SUB, VERSION, '' AS FBCONTENT FROM ICC_FOLDERS WHERE  MANDANT=:MANDANT: "
                  + dwData["SUB"].ToString()
                + " ORDER BY 3,4,5,6";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData); 
            SResult = Connect2Proxy(or1);
            SResult.Tables[0].TableName = TableName;
            return SResult;
        }
        
        public DataSet QFLASTUPDATE(DataRow dwData)
        {
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "SELECT MAX(VERSION) AS VERSION FROM ICC_FOLDERS where version <>'FOLDERS' AND  MANDANT=:MANDANT: AND COMPANY=:COMPANY:";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            SResult = Connect2Proxy(or1);
            SResult.Tables[0].TableName = TableName;
            return SResult;
        }
        public DataSet QFILE(DataRow dwData)
        {
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "SELECT * FROM ICC_FOLDERS WHERE  MANDANT=:MANDANT: AND ROOT=:ROOT:   AND SUB=:SUB:   ";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            SResult = Connect2Proxy(or1);
            SResult.Tables[0].TableName = TableName;
            return SResult;
        }
        public DataSet QFILELINKER(DataRow dwData)
        {
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "SELECT * FROM ICC_FOLDERS WHERE  MANDANT=:MANDANT:   AND LINKER=:LINKER:";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            SResult = Connect2Proxy(or1);
            SResult.Tables[0].TableName = TableName;
            return SResult;
        }
        public void DFOLDERS01(DataRow dwData)
        {

            or1 = OracleDB.CreateCommand();
            or1.CommandText = "DELETE FROM ICC_FOLDERS   WHERE  MANDANT=:MANDANT: AND ROWNID=:ROWNID:  ";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            Connect2Proxy(or1);
        }
        public void DFOLDERS02(DataRow dwData)
        {

            or1 = OracleDB.CreateCommand();
            or1.CommandText = "DELETE FROM ICC_FOLDERS   WHERE  MANDANT=:MANDANT: AND ROOT=:ROOT: AND SUB =:SUB:";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            Connect2Proxy(or1);
        }

        public void UpdateICC_FOLDERS(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        { 
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "UPDATE ICC_FOLDERS SET MANDANT=:MANDANT:,ROWNID=:ROWNID:,LINKER=:LINKER:,COMPANY=:COMPANY:,ROOT=:ROOT:,SUB=:SUB:,FBCONTENT=:FBCONTENT"
                        + " WHERE  MANDANT =:MANDANT: AND ROWNID =:ROWNID:";
                  
                    or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
                    string strOriginText = dwData["FBCONTENT"].ToString();
                    or1.Parameters.Add("FBCONTENT", OracleType.Blob).Value = Helper.CompressString(strOriginText);
                    break;
                default:
                    break;
            }  
            Connect2Proxy(or1);
        }
        public void UFOLDER02(DataRow dwData)
        { 
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "UPDATE  ICC_FOLDERS SET ROOT=REPLACE(ROOT,:ROOT:,'D:\\ITXRPRT')"
                + " WHERE MANDANT=:MANDANT:  AND ROWNID =:ROWNID:";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            Connect2Proxy(or1);
        }
        public void UFOLDER03(DataRow dwData)
        {
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "UPDATE  ICC_FOLDERS SET LINKER=:LINKER:"
                + " WHERE MANDANT=:MANDANT:  AND ROWNID =:ROWNID:";
            or1.CommandText = INTEXDBC.InjectData(or1.CommandText, dwData);
            Connect2Proxy(or1);
        }
    }
}
